import rouetrAdd from "@/router/pageOptions";
import router from '@/router/index';
import Layout from "@/layout/index.vue"
import {getRouterAuthor} from '@/api/login'
import tool from "@/common/tool.js"
import stotr from "@/store"

let routerControl =function(){
    this.obj = {};
    let userMess = tool.getStorage("userMess"),mess = JSON.parse(userMess);
    this.buttonControl = {};
    this.beforeRouterList = tool.deepClone(rouetrAdd)
    this.roleId = mess.currentRole.roleId
}
routerControl.prototype.routerList=function(){
    let params = {
        roleId:this.roleId,
        clientType:1,
        permissionTypes:[1001,1002,1003]
    }
    getRouterAuthor(params).then(res =>{
       console.log("菜单列表",JSON.parse(JSON.stringify(res)));
       this.routerEach(res);
       tool.setStorage({"buttonControl": JSON.stringify(this.buttonControl)});
    //    console.log('权限列表',this.obj,this.buttonControl);
    //    console.log('开始循环列表',this.beforeRouterList);
       this.routerChange(this.beforeRouterList);
    //    console.log('最终列表',this.beforeRouterList);
       this.routerAdd(this.beforeRouterList);
       tool.setStorage({"rouList": JSON.stringify(this.beforeRouterList)})
    })
}
routerControl.prototype.routerEach=function(mess,parentName){
    mess.map(item=>{
        if(item.id == "7"){
            item.childPermission.forEach(element => {
                if(element.permissionCode == 'customerVisit'){
                    tool.setStorage({"customerVisitFlag": element.selected})
                }
            });
            item.childPermission.forEach(element => {
                if(element.permissionCode == 'consultantPoints'){
                    tool.setStorage({"consultantPointsFlag": element.selected})
                }
            });
        }
        if(item.selected == "true"){            
           if(item.permissionType == '1001')this.obj[item.permissionCode] = {permissionTitle:item.permissionTitle,id:item.permissionTitle,permissionCode:item.permissionCode};
           else {
             let name = parentName?parentName:item.permissionTitle;
             !this.buttonControl[name] && (this.buttonControl[name]=[]);
             this.buttonControl[name].push(item.id);
           }
           item.childPermission && this.routerEach(item.childPermission,parentName?parentName:item.permissionTitle)
        }
  })
}
routerControl.prototype.routerChange=function(mess){
  for (let index = mess.length-1; index >= 0; index--) {
     if(mess[index].meta.noAuthor)continue;
     else if(!this.obj[mess[index].path])mess.splice(index, 1);   
     else  mess[index].children && this.routerChange(mess[index].children)
  }
}
routerControl.prototype.routerAdd=function(mess,flag){
    let routeList = [
        {
            path: '/',
            component:Layout,
            children:mess
        },
        {
            path: '*',
            redirect: 's/statistics',
            meta: {
               title: "404",
            }
        },
    ]
    //router.matcher = createRouter(routeList).matcher
    stotr.dispatch("setRouterList",mess);
    //!flag && router.push('/customerManagement_center/allCustomers/index')
    // router.addRoutes(routeList);
    setTimeout(_=>{
        !flag && router.push('/home/statistics')
        //flag && router.push(flag)
    },500)
    
}
routerControl.prototype.routerAddReflash=function(){
    let rouetrAdd = tool.getStorage("rouList");
    rouetrAdd = JSON.parse(rouetrAdd);
    this.routerAdd(rouetrAdd,true)
}
export  const beginAddRouter = (flag)=>{
    if(flag)new routerControl().routerAddReflash(flag);
    else new routerControl().routerList();
}
export  const routerChecked = (router)=>{
   return new Promise(function(resove,reject){
     let flag = false,{name,path}=router,list = stotr.state.routerList;
     let routerEach = (mess,father)=>{
        mess.map(item=>{
            father && (father = father+"/");
            if((item.name && item.name == name) || ((father + item.path) == path))flag = true;
            if(!flag && item.children)routerEach(item.children,item.path)
        })
     }
     routerEach(list);
    flag?resove():reject()
   })
}